/*
 * Created on Oct 5, 2003
 *
 * Mihai Postelnicu
 * 
 */
package ro.gateway.aida.struts.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.tiles.actions.TilesAction;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.db.TestDB;
import ro.gateway.aida.utils.Utils;

/**
 * @author Mihai Postelnicu
 * HomeAction
 *  
 * */
public class HomeAction extends TilesAction {
		public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			ServletContext sc = getServlet().getServletContext();
			
			//prevent caching
			response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
			response.setHeader("Pragma","no-cache"); //HTTP 1.0
			response.setDateHeader ("Expires", -1);
			
			HttpSession s=request.getSession();
			
			//check db connection 
		    PersistenceToken token = PersistenceManager.tokenLookup(sc);
		    if (token==null) return mapping.findForward("dbconfig");
		    TestDB testdb = TestDB.getManager(token);
		    if (!testdb.isDBConnectionValid()) 
		        return mapping.findForward("dbconfig");    
			
			if (!s.getAttributeNames().hasMoreElements()) 
				return mapping.findForward("plslogin");
			Utils.setBreadCrumbs(request,"bc.home");	
			return mapping.findForward("homepage");
		}
}
